<template>
  <ele-modal
    form
    width="90%"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        v-model:selections="selections"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        :toolbar="false"
        :pagination="false"
        border
      >
        <template #img="{ row }">
          <span
            v-if="row.images !== ''"
            class="pointer blue"
            @click="openImages(row)"
            >查看</span
          >
          <span v-else>无</span>
        </template>
      </ele-pro-table>
      <el-button style="margin-top: 18px" type="primary" @click="choose(null)">
        选择
      </el-button>
    </ele-card>
    <!-- 图片 -->
    <images v-model="showImages" :data="current" />
  </ele-modal>
</template>

<script setup>
  import { ref, computed, watch } from 'vue';
  import { pageInsurance } from '@/api/company';
  import Images from '@/views/insurance/components/images.vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,

    vehicleId: Number,
    plateNumber: String
  });
  const emit = defineEmits(['done', 'update:modelValue']);
  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  /** 表格实例 */
  const tableRef = ref(null);
  /** 表格选中数据 */
  const selections = ref([]);
  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push({
      type: 'selection',
      columnKey: 'selection',
      width: 50,
      align: 'center',
      fixed: 'left'
    });

    cols.push(
      {
        width: 70,
        prop: 'id',
        label: '编号'
      },
      {
        width: 80,
        prop: 'cate',
        label: '类型'
      },
      {
        width: 200,
        prop: 'remark',
        label: '报价要求'
      },
      {
        prop: 'insurance',
        label: '保险公司'
      },
      // {
      //   width: 120,
      //   prop: 'insurance_salesman',
      //   label: '业务员'
      // },
      {
        width: 100,
        prop: 'rebate',
        label: '返点'
      },
      {
        width: 120,
        prop: 'price',
        label: '价格'
      },
      {
        width: 210,
        prop: 'sTime',
        label: '开始日期',
        align: 'center'
      },
      {
        width: 210,
        prop: 'eTime',
        label: '结束日期',
        align: 'center'
      },
      {
        width: 150,
        prop: 'createDate',
        label: '报价日期'
      },
      {
        width: 100,
        slot: 'img',
        label: '报价图片',
        align: 'center'
      }
    );

    return cols;
  });

  // 选择
  const choose = (row) => {
    const list = selections.value;
    var c = 0;
    var b = 0;
    for (let i = 0; i < list.length; i++) {
      const v = list[i];
      if (v.cate === '商业险') b++;
      if (v.cate === '交强险') c++;
    }
    if (c > 1) {
      EleMessage.warning('只能选择1条交强险报价出单');
      return false;
    }
    if (b > 1) {
      EleMessage.warning('只能选择1条商业险报价出单');
      return false;
    }
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    emit('done', list);
  };

  /** 表格数据源 */
  const datasource = async ({ page, where, orders }) => {
    const result = await pageInsurance({
      ...where,
      ...orders,
      page,
      limit: 9999,
      vehicle_id: props.vehicleId,
      status: 0
    });
    return result;
  };
  const current = ref([]);

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };

  const title = ref('');
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = props.plateNumber + '的保险报价列表';
        console.log('打开');
      } else {
        console.log('关闭了');
      }
    }
  );
</script>
